#include "Timer.h"
#include "lpc17xx_systick.h" 
#include "lpc17xx_pinsel.h"
#include "lpc17xx_timer.h"

void initSysTimer(uint16_t ms)
{
    SYSTICK_ClearCounterFlag();
    SYSTICK_InternalInit(ms);
    SYSTICK_Cmd(ENABLE);
    SYSTICK_IntCmd(ENABLE); 
    
}

void initTimer0(uint16_t ms)
{
    PINSEL_CFG_Type PinCfg;
    PinCfg.Funcnum = 3;
    PinCfg.OpenDrain = 0;
    PinCfg.Pinmode = 0;
    PinCfg.Portnum = 1;
    PinCfg.Pinnum = 28;
    PINSEL_ConfigPin(&PinCfg);
    
    TIM_TIMERCFG_Type TIM_ConfigStruct;
    TIM_MATCHCFG_Type TIM_MatchConfigStruct ;
    TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
    TIM_ConfigStruct.PrescaleValue  = 10;
    TIM_MatchConfigStruct.MatchChannel = 0;
    TIM_MatchConfigStruct.IntOnMatch   = TRUE;
    TIM_MatchConfigStruct.ResetOnMatch = TRUE;
    TIM_MatchConfigStruct.StopOnMatch  = FALSE;
    TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_NOTHING;
    TIM_MatchConfigStruct.MatchValue   = ms;
    TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
    TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);
    NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3) | 0x01));
    NVIC_EnableIRQ(TIMER0_IRQn);
    TIM_Cmd(LPC_TIM0,ENABLE);
}

void dumbAssTimer(uint32_t us)
{
    us *= 100;
    for(us; us>0; --us)
    --us;
}
